;
;	    Nascom  hardware support
;

            .module nascom

            ; exported symbols
            .globl init_early
            .globl init_hardware
            .globl interrupt_handler
            .globl _program_vectors
	    .globl platform_interrupt_all
	    .globl _nmikey
	    .globl _int_disabled

            ; exported debugging tools
            .globl _platform_monitor
            .globl _platform_reboot
            .globl outchar

            ; imported symbols
            .globl _ramsize
            .globl _procmem
            .globl istack_top
            .globl istack_switched_sp
            .globl unix_syscall_entry
            .globl outcharhex
	    .globl null_handler
	    .globl map_kernel
	    .globl map_process
	    .globl map_process_a
	    .globl map_process_always
	    .globl map_save_kernel
	    .globl map_restore

	    .globl s__COMMONMEM
	    .globl l__COMMONMEM

	    .globl _bufpool

            .include "kernel.def"
            .include "../kernel-z80.def"

	    .area _BUFFERS

_bufpool:
	    .ds BUFSIZE * NBUFS
; -----------------------------------------------------------------------------
; COMMON MEMORY BANK (0xE800 upwards)
; -----------------------------------------------------------------------------
            .area _COMMONMEM

platform_interrupt_all:
	    ret

_platform_monitor:
_platform_reboot:
	    di
	    jr _platform_reboot

_int_disabled:
	   .db 1

; -----------------------------------------------------------------------------
; KERNEL MEMORY BANK (below 0xE800, only accessible when the kernel is mapped)
; -----------------------------------------------------------------------------
            .area _CODE

init_early:
            ret

;------------------------------------------------------------------------------
; COMMON MEMORY PROCEDURES FOLLOW

            .area _COMMONMEM

_program_vectors:
            ; we are called, with interrupts disabled, by both newproc() and crt0
	    ; will exit with interrupts off
            di ; just to be sure
            pop de ; temporarily store return address
            pop hl ; function argument -- base page number
            push hl ; put stack back as it was
            push de

	    call map_process

            ; write zeroes across all vectors
            ld hl, #0
            ld de, #1
            ld bc, #0x007f ; program first 0x80 bytes only
            ld (hl), #0x00
            ldir

            ; now install the interrupt vector at 0x0038
            ld a, #0xC3 ; JP instruction
            ld (0x0038), a
            ld hl, #interrupt_handler
            ld (0x0039), hl

            ; set restart vector for UZI system calls
            ld (0x0030), a   ;  (rst 30h is unix function call vector)
            ld hl, #unix_syscall_entry
            ld (0x0031), hl

            ld (0x0000), a   
            ld hl, #null_handler   ;   to Our Trap Handler
            ld (0x0001), hl

	    ; Some Nascoms got fitted with an NMI button - what should it do
	    ; ?
            ld (0x0066), a  ; Set vector for NMI
            ld hl, #nmi_key_handler
            ld (0x0067), hl
	    jp map_kernel

;
;	This isn't absolutely perfect but as close as we can get
;
nmi_key_handler:
	    push af
	    ld a,#1
	    ld (_nmikey),a
	    pop af
	    retn
	    
; outchar: Wait for UART TX idle, then print the char in A
; destroys: AF
outchar:
	    push af
outwait:
	    in a, (0x02)
	    bit 6,a
	    jr z, outwait
            out (0x1), a
            ret

	    .area _COMMONDATA
_nmikey:    .byte 0
